﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.BL;
using ADC.DAL;
using ADC.Utilites;

namespace ADC.Web.Admin
{
    public partial class ManageSlide : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ManageSlide");

        private int _slideID;
        private int _mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitData();
            }

        }

        private void InitData()
        {
            if (Request.Params["mode"] != null)
            {
                try
                {
                    _mode = Convert.ToInt32(Request.Params["mode"].ToString());

                    if (_mode.Equals(1)) // Edit Mode
                    {
                        if ((Request.Params["ID"] == null)) return;
                        _slideID = Convert.ToInt32(Request.Params["ID"].ToString());
                        LinkButtonAddNew.Text = "Cập nhật";
                        lblHeader.Text = "Cập nhật hình ảnh banner";
                        imageLogo.Visible = true;

                        // Get from database
                        var slide = SlideFactory.SelectByID(_slideID);
                        if (slide != null)
                        {
                            txtTitle.Text = slide.Title;
                           
                            imageLogo.ImageUrl = "../" + slide.ImagePath;
                        }
                    }
                    else if (_mode.Equals(0))
                    { // New Mode
                        LinkButtonAddNew.Text = "Thêm mới";
                        lblHeader.Text = "Thêm mới hình ảnh banner";
                        imageLogo.Visible = false;
                    }
                }
                catch (Exception ex)
                {
                    _mode = 0;
                    logger.Error(ex.ToString());
                    if (GlobalInfo.IsEnableDebugMode) throw;
                }
            }
        }

        protected void LinkButtonAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                _mode = Convert.ToInt32(Request.Params["mode"].ToString());
                if (_mode.Equals(0))
                { // Insert
                    var slide = new DAL.Slide();
                    slide.Title = txtTitle.Text;

                    int newID;
                    newID = SlideFactory.Insert(slide);

                    if (FileUpload1.HasFile)
                    {
                        string filePath = Request.PhysicalApplicationPath + Common.IMAGE_BANNER_GALLERY_PATH + "BannerGallery" + newID;
                        if (!System.IO.Directory.Exists(filePath))
                        {
                            System.IO.Directory.CreateDirectory(filePath);
                        }

                        FileUpload1.SaveAs(filePath + "/" + FileUpload1.FileName);
                        // Update customer
                        slide.ImagePath = Common.IMAGE_BANNER_GALLERY_PATH + "BannerGallery" + newID + "/" + FileUpload1.FileName;

                        if (SlideFactory.Update(slide) > 0)
                        {
                            lblErrorMessage.Visible = true;
                            lblErrorMessage.Text = Common.UPDATE_SUCESSFULLY;
                        }
                        else
                        {
                            lblErrorMessage.Visible = true;
                            lblErrorMessage.Text = Common.UPDATE_FAIL;
                        }
                    }


                    Response.Redirect("ListSlide.aspx", false);
                }
                else
                {
                    _slideID = Convert.ToInt32(Request.Params["ID"].ToString());
                    string imagePath;
                    if (FileUpload1.HasFile)
                    {
                        string filePath = Request.PhysicalApplicationPath + Common.IMAGE_BANNER_GALLERY_PATH + "BannerGallery" + _slideID;
                        if (!System.IO.Directory.Exists(filePath))
                        {
                            System.IO.Directory.CreateDirectory(filePath);
                        }
                        // Delete the existing email
                        if (System.IO.File.Exists(filePath + "/" + imageLogo.ImageUrl.Substring(3, imageLogo.ImageUrl.Length - 3)))
                        {
                            System.IO.File.Delete(filePath + "/" + imageLogo.ImageUrl.Substring(0, 3));
                        }
                        FileUpload1.SaveAs(filePath + "/" + FileUpload1.FileName);
                        imagePath = Common.IMAGE_BANNER_GALLERY_PATH + "BannerGallery" + _slideID + "/" + FileUpload1.FileName;
                    }
                    else
                        imagePath = "";

                    // Save to datbase
                    var slide = new DAL.Slide();
                    slide.ImagePath = imagePath;
                    slide.Title = txtTitle.Text;
                   
                    slide.SlideID = _slideID;
                    //SlideFactory.Update(slide);
                    if (SlideFactory.Update(slide) > 0)
                    {
                        lblErrorMessage.Visible = true;
                        lblErrorMessage.Text = Common.UPDATE_SUCESSFULLY;
                    }
                    else
                    {
                        lblErrorMessage.Visible = true;
                        lblErrorMessage.Text = Common.UPDATE_FAIL;
                    }
                    InitData();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListSlide.aspx", false);
        }


    }
}